#import "CacheManager.h"
#import "CellFrame.h"
#import "CellDataModel.h"

static CacheManager* cacheMgr;

@implementation CacheManager

+(CacheManager*)getInstanse{
    if (cacheMgr == nil) {
        cacheMgr = [[CacheManager alloc]init];
    }
    return cacheMgr;
}

/**
    存储dynamic数据实体到tableview的前10条内容到本地
 */
-(BOOL)writeDataToFile_dynamic:(NSMutableArray*) listArr{
    long cache_num = 10;
    if (listArr.count < 10) {
        cache_num = listArr.count;
    }
    NSMutableArray *rArr = [NSMutableArray arrayWithCapacity:cache_num];
    for (long i = cache_num-1; i>-1; i--) {
        CellFrame *cf = [listArr objectAtIndex:i];
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:5];
        [dict setValue:cf.model.icon forKey:@"icon"];
        [dict setValue:cf.model.nickname forKey:@"nickname"];
        [dict setValue:cf.model.content forKey:@"content"];
        [dict setValue:cf.model.timestamp forKey:@"timestamp"];
        [dict setValue:cf.model.image1 forKey:@"image1"];
        [dict setValue:cf.model.image2 forKey:@"image2"];
        [dict setValue:cf.model.image3 forKey:@"image3"];
        [dict setValue:cf.model.image4 forKey:@"image4"];
        [dict setValue:cf.model.image5 forKey:@"image5"];
        [dict setValue:cf.model.image6 forKey:@"image6"];
        [dict setValue:cf.model.image7 forKey:@"image7"];
        [rArr insertObject:dict atIndex:0];
    }
    
    //获取应用程序沙盒的Caches目录
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    
    //得到完整的文件名
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"cellCaches.plist"];
    //输入写入
    [rArr writeToFile:filename atomically:YES];
    
    return YES;
}

-(NSMutableArray *)readDataFromFile{
    //读取plist
    //获取应用程序沙盒的Caches目录
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    //得到完整的文件名
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"cellCaches.plist"];
    
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:filename];
    return data;
}
@end
